<div class="layui-fluid layui-anim febs-anim" id="febs-login-log" lay-title="登录日志">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="login-log-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">登录用户</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="username" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">登录时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="createTime" id="createTime" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="loginlog:delete,loginlog:export">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="loginLogTable" lay-data="{id: 'loginLogTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="login-log-option">
    <span shiro:lacksPermission="loginlog:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="del" shiro:hasPermission="loginlog:delete"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'laydate', 'form', 'table', 'febs', 'dropdown'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#febs-login-log'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'loginTime', type: null},
            tableIns;

        laydate.render({
            elem: '#createTime',
            range: true,
            trigger: 'click'
        });

        form.render();

        initTable();

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                if (name === 'delete') {
                    var checkStatus = table.checkStatus('loginLogTable');
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请勾选需要删除的日志');
                    } else {
                        febs.modal.confirm('删除日志', '确定删除所选日志？', function () {
                            var logIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                logIds.push(item.id)
                            });
                            deleteLogs(logIds.join(','))
                        });
                    }
                }
                if (name === 'export') {
                    var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
                    params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
                    params.pageNum = $view.find(".layui-laypage-em").next().html();
                    febs.download(ctx + 'loginLog/excel', params, '登录日志表.xlsx');
                }
            },
            options: [ {
                name: 'delete',
                title: '删除日志',
                perms: 'loginlog:delete'
            }, {
                name: 'export',
                title: '导出Excel',
                perms: 'loginlog:export'
            }]
        });

        table.on('tool(loginLogTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'del') {
                febs.modal.confirm('删除日志', '确定删除该条登录日志？', function () {
                    deleteLogs(data.id);
                });
            }
        });

        table.on('sort(loginLogTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'loginLogTable',
                url: ctx + 'loginLog/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'username', title: '登录用户'},
                    {field: 'ip', title: 'IP地址'},
                    {field: 'location', title: '登录地点', minWidth: 180},
                    {field: 'loginTime', title: '登录时间', minWidth: 180, sort: true},
                    {field: 'system', title: '登录系统'},
                    {field: 'browser', title: '浏览器'},
                    {title: '操作', toolbar: '#login-log-option'}
                ]]
            });
        }

        function deleteLogs(logIds) {
            febs.get(ctx + 'loginLog/delete/' + logIds, null, function () {
                febs.alert.success('删除登录日志成功');
                $query.click();
            });
        }

        function getQueryParams() {
            var createTimeFrom,
                createTimeTo,
                createTime = $searchForm.find('input[name="createTime"]').val();
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            return {
                loginTimeFrom: createTimeFrom,
                loginTimeTo: createTimeTo,
                username: $searchForm.find('input[name="username"]').val().trim()
            };
        }
    })
</script>